package cn.iocoder.yudao.module.cmt.service.notify;

import cn.iocoder.yudao.module.cmt.dal.mysql.notify.CmtNotifyNoticeMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.notify.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.notify.CmtNotifyNoticeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class CmtNotifyNoticeServiceImpl implements CmtNotifyNoticeService {

    @Resource
    private CmtNotifyNoticeMapper cmtNotifyNoticeMapper;

    @Override
    public Long createNotice(CmtNotifyNoticeSaveReqVO createReqVO) {
        CmtNotifyNoticeDO notice = BeanUtils.toBean(createReqVO, CmtNotifyNoticeDO.class);
        cmtNotifyNoticeMapper.insert(notice);
        return notice.getId();
    }

    @Override
    public void updateNotice(CmtNotifyNoticeSaveReqVO updateReqVO) {
        validateNoticeExists(updateReqVO.getId());
        CmtNotifyNoticeDO updateObj = BeanUtils.toBean(updateReqVO, CmtNotifyNoticeDO.class);
        cmtNotifyNoticeMapper.updateById(updateObj);
    }

    @Override
    public void deleteNotice(Long id) {
        validateNoticeExists(id);
        cmtNotifyNoticeMapper.deleteById(id);
    }

    private void validateNoticeExists(Long id) {
        if (cmtNotifyNoticeMapper.selectById(id) == null) {
            throw exception(NOTICE_NOT_EXISTS);
        }
    }

    @Override
    public CmtNotifyNoticeDO getNotice(Long id) {
        return cmtNotifyNoticeMapper.selectById(id);
    }

    @Override
    public PageResult<CmtNotifyNoticeDO> getNoticePage(CmtNotifyNoticePageReqVO pageReqVO) {
        return cmtNotifyNoticeMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNoticeListByIds(List<Long> ids) {
        ids.forEach(this::validateNoticeExists);
        cmtNotifyNoticeMapper.deleteBatchIds(ids);
    }

}
